package com.backend.securitiessystem.result;

import com.backend.securitiessystem.enums.AppHttpCodeEnum;
import io.swagger.annotations.ApiModel;
import lombok.Data;

/**
 * 统一封装结果类
 *
 * @author qksuki
 * &#064;date  2024-05-19 07:22:50
 */
@Data
@ApiModel(value = "Result", description = "统一封装结果类")
public class Result<T> {
    private Integer code;

    private String msg;

    private T data;

    public Result() {
        this.code = 200;
    }

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> errorResult(int code, String msg) {
        Result<T> result = new Result<>();
        return result.error(code, msg);
    }

    public static <T> Result<T> success(int code, String msg) {
        Result<T> result = new Result<>();
        return result.ok(code, null, msg);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static <T> Result<T> errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMsg());
    }

    public static <T> Result<T> errorResult(AppHttpCodeEnum enums, String errormsg) {
        return setAppHttpCodeEnum(enums, errormsg);
    }

    public static <T> Result<T> setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return success(enums.getCode(), enums.getMsg());
    }

    private static <T> Result<T> setAppHttpCodeEnum(AppHttpCodeEnum enums, String errormsg) {
        return success(enums.getCode(), errormsg);
    }

    public Result<T> error(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public Result<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public Result<T> ok(Integer code, T data, String msg) {
        Result<T> result = new Result<>();
        result.code = code;
        result.data = data;
        result.msg = msg;
        return result;
    }

    public Result<?> ok(T data) {
        this.data = data;
        return this;
    }
}

